<?php
namespace app\controllers;

use app\models\AmountRecorder;
use app\models\CouponRecorder;
use app\models\DiscountRecorder;
use app\models\GoodsRecorder;
use app\models\MemberRecorder;
use app\models\NewRecorder;
use app\models\OrderRecorder;
use app\models\PurchaseRecorder;

class TjController extends BaseController{
    public function actionTjnum(){
        $post = \Yii::$app->request->post('form');
        $sl_time = $post['sl_time'];
        $type = intval($post['type']);
        $type = empty($type)?1:$type;
        $start_time=empty($sl_time)?strtotime("-6 month",time()):strtotime($sl_time[0]);
        $end_time=empty($sl_time)?time():strtotime($sl_time[1]);
        $list = OrderRecorder::find()->select(['addtime','total_price']);
        $list = $list->where("addtime between {$start_time} and {$end_time}")->asArray()->all();
        $date=[];
        $price=[];
        $num=[];
        foreach($list as $k=>$v){
            $time = $type==1?date('Y-m',$v['addtime']):date('Y',$v['addtime']);
            if(in_array($time,$date)){
                $index = array_search($time,$date);
                $price[$index]+=$v['total_price'];
                $num[$index]++;
            }else{
                $date[] = $time;
                $price[]=$v['total_price'];
                $num[]=1;
            }
        }

        $result = ['result'=>1,'date'=>$date,'price'=>$price,'num'=>$num];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionTjgoods(){
        $sl_time = \Yii::$app->request->post('time');
        $sl_num = \Yii::$app->request->post('num');
        $sl_num = abs(intval($sl_num));
        $sl_num = empty($sl_num)?5:$sl_num;
        $start_time=empty($sl_time)?strtotime("-6 month",time()):strtotime($sl_time[0]);
        $end_time=empty($sl_time)?time():strtotime($sl_time[1]);
        $list = OrderRecorder::find()->select('goods_attr');
        $list = $list->where("addtime between {$start_time} and {$end_time}")->asArray()->all();

        $goods=[];
        $num=[];
        $price=[];
        foreach($list as $k=>$v){
            $attr = json_decode($v['goods_attr'],true);
            foreach($attr as $ak=>$av){
                if(in_array($av['id'],$goods)){
                    $index = array_search($av['id'],$goods);
                    $num[$index]+=$av['num'];
                    $price[$index]=$price[$index]+empty($av['attr_price'])?0:$av['attr_price'];
                }else{
                    $goods[]=$av['id'];
                    $num[]=$av['num'];
                    $price[]=empty($av['attr_price'])?0:$av['attr_price'];
                }
            }
        }


        $arr=[];
        foreach($goods as $k=>$v){
            if($k>=$sl_num){
                break;
            }
            $model = GoodsRecorder::find()->select(['id','goods_name','goods_price'])->where('id='.$v)->one();
            $arr[$k]['id'] = $model->id;
            $arr[$k]['num'] = $num[$k];
            $arr[$k]['goodsname'] = $model->goods_name;
            $arr[$k]['price'] = $price[$k];
            $jj = empty($num[$k])?0:($price[$k]/$num[$k]);
            $arr[$k]['jj_price'] = sprintf("%.2f",$jj);
        }
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionTjmember(){
        $post = \Yii::$app->request->post('form');
        $sl_time = $post['sl_time'];
        $sl_num = intval($post['num']);
        $sl_num = ($sl_num<=0)?5:$sl_num;
        $start_time=empty($sl_time)?strtotime("-6 month",time()):strtotime($sl_time[0]);
        $end_time=empty($sl_time)?time():strtotime($sl_time[1]);
        $list = OrderRecorder::find()->select('user_id,count(id) as num,sum(total_price) as price');
        $list = $list->where("addtime between {$start_time} and {$end_time}")->groupBy('user_id')->orderBy('price DESC')->asArray()->all();
        foreach($list as $k=>$v){
            if($k>=$sl_num){
                return;
            }
            $model = MemberRecorder::find()->select('id,user_name')->where('id='.$v['user_id'])->one();
            $list[$k]['user_name'] = empty($model)?'未知':$model->user_name;

        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionOrderinfo(){
        $order = OrderRecorder::find()->select('count(id) as num,sum(total_price)  as price')->where('is_pay=1')->asArray()->one();
        $goods = GoodsRecorder::find()->select('sum(hits) as hits')->one();
        $order['hits'] = $goods->hits;
        $djv = empty($order['hits'])?0:(1000/$order['hits']);
        $order['q_num'] = intval($order['num']*$djv);
        $order['q_price'] = $order['price']*$djv;
        $order['q_price'] = sprintf("%.2f",$order['q_price']);
        $result = ['result'=>1,'data'=>$order];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionTjorder(){
        $post = \Yii::$app->request->post('form');
        $sl_time = $post['sl_time'];
        $type = intval($post['type']);
        $type = empty($type)?1:$type;
        $start_time=empty($sl_time)?strtotime("-6 month",time()):strtotime($sl_time[0]);
        $end_time=empty($sl_time)?time():strtotime($sl_time[1]);
        $order = OrderRecorder::find()->where("addtime between {$start_time} and {$end_time}");
        //概况
        $arr=[];
        if($type==1) {
            $pay_order = $order;
            $arr[0]['name'] = "已支付";
            $arr[0]['value'] = $pay_order->where('is_pay=1')->count();
            $arr[1]['name'] = "未支付";
            $arr[1]['value'] = $order->where('is_pay=0')->count();
            $return_arr=$arr;
        }elseif($type==2){//营销
            $yh_order = $order;
            $list = $order->select('yx')->andWhere('is_pay=1')->asArray()->all();
            $yx_arr=[0,0,0,0];
            foreach($list as $k=>$v){
                $yx = json_decode($v['yx'],true);
                foreach($yx as $v){
                    switch($v['type']){
                        case 2:
                            $yx_arr[0]++;
                            break;
                        case 3:
                            $yx_arr[1]++;
                            break;
                        case 4:
                            $yx_arr[2]++;
                            break;
                        case 5:
                            $yx_arr[3]++;
                            break;
                    }
                }
            }
            $return_arr=[];
            $name_arr=['满件减','满额减','限时购','新人购'];
            foreach($yx_arr as $k=>$v){
                $return_arr[$k]['name']=$name_arr[$k];
                $return_arr[$k]['value']=$v;
            }
            $yh_arr['name'] = "优惠券";
            $yh_arr['value'] = $yh_order->andWhere('is_pay=1')->andWhere('coupon_id!=0')->count();

            $return_arr[]=$yh_arr;

        }

        $result = ['result'=>1,'data'=>$return_arr];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionSell(){
        $post = \Yii::$app->request->post('form');
        $cate = intval($post['cate']);
        $num = intval($post['num']);
        $num = empty($num)?20:$num;
        $list  = GoodsRecorder::find();
        if(!empty($cate)){
            $list = $list->where('cate_id=:cate',[':cate' => $cate]);
        }
        $list = $list->orderBy('sell_num DESC')->limit($num)->asArray()->all();
        $arr=[];
        foreach($list as $k=>$v){
            $arr[$k]['id']=$v['id'];
            $arr[$k]['goods_name'] = $v['goods_name'];
            $arr[$k]['sell_num'] = $v['sell_num'];
            $hits = $v['hits']-$v['sell_num'];
            $arr[$k]['hits'] = $hits<0?0:$hits;
            $dj = empty($v['hits'])?0:($v['sell_num']/$v['hits']);
            $dj=empty($dj)?0:sprintf("%.2f",$dj);
            $arr[$k]['dj'] = $dj;
        }
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

}
